//
//  SupplenessTestResultViewController.m
//  安好
//
//  Created by qinlongqing on 14/11/28.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "SupplenessTestResultViewController.h"
#import "UserManager.h"
#import "AppConfig.h"

@interface SupplenessTestResultViewController ()
{
    UIImageView *levelImageView;
    UIImage *_imageV;
    
    
    CGFloat nameAndFenShuLabelHeight;
    CGFloat levelViewHeight;
    CGFloat tiShiLabelHeight;
    CGFloat dateAndSealViewHeight;
    CGFloat shareBTNHeight;
}
@end

@implementation SupplenessTestResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"测试报告";
    
    self.view.backgroundColor = UIColorFromRGB(0xe7f1f4);
    
    [self makeNavBar];
    
    if (kScreenWidth == 414) {
        nameAndFenShuLabelHeight = 36;
        //        levelViewHeight =
        tiShiLabelHeight = 48;
        dateAndSealViewHeight = 96;
        shareBTNHeight = 54;
    }
    else{
        nameAndFenShuLabelHeight = 30;
        //        levelViewHeight =
        tiShiLabelHeight = 40;
        dateAndSealViewHeight = 80;
        shareBTNHeight = 45;
    }
    CGFloat space = (kScreenHeight - 64 - kScreenWidth*100/320 - nameAndFenShuLabelHeight - tiShiLabelHeight - dateAndSealViewHeight - shareBTNHeight)/6;

    
    UILabel *nameAndFenShuLabel = [[UILabel alloc]init];
    nameAndFenShuLabel.frame = CGRectMake(0, space, kScreenWidth, nameAndFenShuLabelHeight);
    if (kScreenWidth == 414) {
        nameAndFenShuLabel.font = [UIFont systemFontOfSize:20];
    }
    else{
        nameAndFenShuLabel.font = [UIFont systemFontOfSize:18];
    }
    nameAndFenShuLabel.textAlignment = NSTextAlignmentCenter;
    nameAndFenShuLabel.textColor = [UIColor blackColor];
    nameAndFenShuLabel.adjustsFontSizeToFitWidth = YES;
    nameAndFenShuLabel.backgroundColor = [UIColor clearColor];
    NSString *showStr = nil;
    if ([self.testResultDict valueForKey:kTestUserName]) {//有用户
        showStr = [NSString stringWithFormat:@"%@的柔韧性测试结果是:%ld分",[self.testResultDict valueForKey:kTestUserName],[AppConfig getSupplenessTestScoreWithTestKeepedTime:[[self.testResultDict valueForKey:kRouRenXingTestKeepedTime] floatValue]]];
    }
    else{
        showStr = [NSString stringWithFormat:@"您的柔韧性测试结果是:%ld分",[AppConfig getSupplenessTestScoreWithTestKeepedTime:[[self.testResultDict valueForKey:kRouRenXingTestKeepedTime] floatValue]]];
    }
    NSRange leftRange = [showStr rangeOfString:@"的柔韧性测试结果是:"];
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:showStr];
    [attributedString addAttribute:NSForegroundColorAttributeName value:UIColorFromRGB(0x4fc1e9) range:NSMakeRange(leftRange.location+leftRange.length, showStr.length - leftRange.location - leftRange.length)];
    nameAndFenShuLabel.attributedText = attributedString;
    [self.view addSubview:nameAndFenShuLabel];
    
    //level相关
    UIView *levelView = [[UIView alloc]init];
    levelView.frame = CGRectMake(0, nameAndFenShuLabel.frame.origin.y + nameAndFenShuLabel.frame.size.height + space, kScreenWidth, kScreenWidth*100/320);
    //    if (IPHONE5_OR_LATER) {
    //        levelView.frame = CGRectMake(0, 90, 320, 100);
    //    }
    //    else{
    //        levelView.frame = CGRectMake(0, 70, 320, 100);
    //    }
    levelImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"test_level"]];
    levelImageView.frame = CGRectMake(15, levelView.frame.size.height/2, kScreenWidth - 30, (kScreenWidth - 30)*50/290);
    [levelView addSubview:levelImageView];
    UIImageView *positionImageView = [[UIImageView alloc]init];
    CGFloat position_X = [self getPositionImageView_X];
    if (position_X < levelImageView.frame.size.width/2) {
        positionImageView.image = [UIImage imageNamed:@"test_position_left"];
        positionImageView.frame = CGRectMake(position_X - 15 + 17, levelView.frame.size.height/2*15/50, 100, 33);
        if (kScreenWidth == 375) {
            CGRect rect = positionImageView.frame;
            rect.origin.y = rect.origin.y + 5;
            positionImageView.frame = rect;
        }
        else if (kScreenWidth == 414){
            CGRect rect = positionImageView.frame;
            rect.origin.y = rect.origin.y + 10;
            positionImageView.frame = rect;
        }
    }
    else{
        positionImageView.image = [UIImage imageNamed:@"test_position_right"];
        positionImageView.frame = CGRectMake(position_X - 85 + 13, levelView.frame.size.height/2*15/50, 100, 33);
        if (kScreenWidth == 375) {
            CGRect rect = positionImageView.frame;
            rect.origin.y = rect.origin.y + 5;
            positionImageView.frame = rect;
        }
        else if (kScreenWidth == 414){
            CGRect rect = positionImageView.frame;
            rect.origin.y = rect.origin.y + 10;
            positionImageView.frame = rect;
        }
    }
    [levelView addSubview:positionImageView];
    [self.view addSubview:levelView];

    
    //提示语
    UILabel *tiShiLabel = [[UILabel alloc]init];
    if (kScreenWidth == 414) {
        tiShiLabel.frame = CGRectMake(20, levelView.frame.origin.y + levelView.frame.size.height + space, kScreenWidth - 40, tiShiLabelHeight);
        tiShiLabel.font = [UIFont systemFontOfSize:17];
    }
    else{
        tiShiLabel.frame = CGRectMake(15, levelView.frame.origin.y + levelView.frame.size.height + space, kScreenWidth - 30, tiShiLabelHeight);
        tiShiLabel.font = [UIFont systemFontOfSize:15];
    }
    tiShiLabel.textAlignment = NSTextAlignmentCenter;
    tiShiLabel.textColor = [UIColor blackColor];
    tiShiLabel.lineBreakMode = NSLineBreakByWordWrapping;
    tiShiLabel.numberOfLines = 2;
    tiShiLabel.text = [AppConfig getSupplenessPingYuWithTestKeepedTime:[[self.testResultDict valueForKey:kRouRenXingTestKeepedTime] floatValue]];
    tiShiLabel.backgroundColor = [UIColor clearColor];
    [self.view addSubview:tiShiLabel];

    //---------------------时间和盖章----------------------
    UIView *dateAndSealView = [[UIView alloc]init];
    dateAndSealView.frame = CGRectMake(0, tiShiLabel.frame.origin.y + tiShiLabel.frame.size.height + space, kScreenWidth, dateAndSealViewHeight);
    [self.view addSubview:dateAndSealView];
    
    NSDate *dates = [NSDate date];
    NSDateFormatter *formatter =  [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy年MM月dd日  HH:mm"];
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/beijing"];
    [formatter setTimeZone:timeZone];
    NSString *loctime = [formatter stringFromDate:dates];
    
    UILabel *dateLabel = [[UILabel alloc]init];
    if (kScreenWidth == 414) {
        dateLabel.frame = CGRectMake(kScreenWidth*(170.0/320.0), 50, 168, 36);
        dateLabel.font = [UIFont systemFontOfSize:15];
    }
    else{
        dateLabel.frame = CGRectMake(kScreenWidth*(170.0/320.0), 45, 140, 30);
        dateLabel.font = [UIFont systemFontOfSize:13];
    }
    dateLabel.backgroundColor = [UIColor clearColor];
    dateLabel.textAlignment = NSTextAlignmentCenter;
    dateLabel.textColor = [UIColor grayColor];
    dateLabel.text = loctime;
    [dateAndSealView addSubview:dateLabel];
    
    UIImageView *sealView = [[UIImageView alloc]init];
    if (kScreenWidth == 414) {
        sealView.frame = CGRectMake(kScreenWidth*(233.0/320.0), 0, 80, 80);
    }
    else{
        sealView.frame = CGRectMake(kScreenWidth*(233.0/320.0), 0, 67, 67);
    }
    sealView.image = [UIImage imageNamed:@"ce_shi_bao_gao_seal"];
    [dateAndSealView addSubview:sealView];
    
    //分享
    UIButton *shareBTN = [[UIButton alloc]init];
    shareBTN.layer.cornerRadius = 3;
    if (kScreenWidth == 414) {
        shareBTN.frame = CGRectMake(20, dateAndSealView.frame.origin.y + dateAndSealView.frame.size.height + space, kScreenWidth - 40, shareBTNHeight);
    }
    else{
        shareBTN.frame = CGRectMake(15, dateAndSealView.frame.origin.y + dateAndSealView.frame.size.height + space, kScreenWidth - 30, shareBTNHeight);
    }
    [shareBTN setBackgroundImage:[UIImage imageNamed:@"tiJianFenXiangBG"] forState:UIControlStateNormal];
    [shareBTN setBackgroundImage:[UIImage imageNamed:@"tiJianFenXiangBG"] forState:UIControlStateHighlighted];
    [shareBTN setTitle:@"分享" forState:UIControlStateNormal];
    [shareBTN setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [shareBTN addTarget:self action:@selector(doToShare:) forControlEvents:UIControlEventTouchUpInside];
    if ([[UserManager shareInstance] getUserIsLogin].length > 0) {
        shareBTN.hidden = NO;
    }
    else{
        shareBTN.hidden = YES;
    }
    [self.view addSubview:shareBTN];

}

- (void)makeNavBar{
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(doBack)];
    [backBtnImg setTintColor:[UIColor whiteColor]];
    self.navigationItem.leftBarButtonItem = backBtnImg;
}

-(void)doBack{
    [self.navigationController popToViewController:self.supplenessViewController animated:YES];
}

- (CGFloat)getPositionImageView_X{
    CGFloat currentTestKeepedTime = [[self.testResultDict valueForKey:kRouRenXingTestKeepedTime] floatValue];
    CGFloat levelImageTotalWidth = levelImageView.frame.size.width;
    
    if (currentTestKeepedTime > 0.0 && currentTestKeepedTime <= 60.0) {//一分钟
        return (CGFloat)(currentTestKeepedTime/60.0)*(levelImageTotalWidth/3.0);
    }
    else if (currentTestKeepedTime > 60.0 && currentTestKeepedTime <= 120.0){//两分钟
        return (CGFloat)((currentTestKeepedTime - 60.0)/60.0)*(levelImageTotalWidth/3.0) + (levelImageTotalWidth/3.0);
    }
    else if (currentTestKeepedTime > 120.0){//两分钟以后
        CGFloat positiom_x = ((currentTestKeepedTime - 120.0)/60.0)*(levelImageTotalWidth/3.0) + (levelImageTotalWidth/3.0)*2;
        if (positiom_x > 290) {
            positiom_x = 290;
        }
        return positiom_x;
    }
    return 0;
}

- (void)doToShare:(UIButton *)sender{
    CGRect rect = CGRectMake(0, 0, kScreenWidth, sender.frame.origin.y);
//    if (IPHONE5_OR_LATER) {
//        rect = CGRectMake(0, 0, 320, 375);
//    }
//    else{
//        rect = CGRectMake(0, 0, 320, 325);
//    }
    
    [self imageFromView:self.view atFrame:rect];
    
    //分享
    [self showShareView];
}

- (void)imageFromView:(UIView *)theView atFrame:(CGRect)r
{
    UIGraphicsBeginImageContext(theView.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [theView.layer renderInContext:context];
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    CGImageRef imageRef = theImage.CGImage;
    CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, r);//原图中抠出来部分图
    UIImage *subImage = [UIImage imageWithCGImage:subImageRef];//新图
    _imageV = subImage;
}
#pragma 实现share方法，通过点击的视图的tag值判断是什么类型的分享
- (void)share:(UITapGestureRecognizer *)shareTap{
    switch (shareTap.view.tag) {
        case 11:{//微信
            [self weixinShare:kWeiXinShareUrl :kWeiXinShareTitle :kWeiXinShareText :_imageV];
        }
            break;
        case 12:{//朋友圈
            [self weixinFriendShare:kWeiXinFriendShareUrl :kWeiXinFriendShareTitle :kWeiXinFriendShareText :_imageV];
        }
            break;
        case 13:{//短信
            [self showSMSPicker:kWeiXinFriendShareUrl :kWeiXinFriendShareTitle :kWeiXinFriendShareText :nil];
        }
            break;
        case 14:{//QQ
            [self qqShare:kQQshareUrl :kQQshareTitle :kQQshreDescription :_imageV];
        }
            break;
        case 15:{//QQ空间
            [self qqZoneShare:kQQZoneShareUrl :kQQZoneShareTitle :kQQZoneShareDescription :_imageV];
        }
            break;
        case 16:{//新浪微博
            [self sinaWeiboShare:kSinaWeiboShareUrl :kSinaWeiboShareTitle :kSinaWeiboShareText :_imageV];
        }
            break;
            
        default:
            break;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
